package com.dhcc.core.api.vo;

import java.util.Date;
import java.util.List;

import org.springframework.format.annotation.DateTimeFormat;

import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.core.modules.system.cache.dept.DeptCache;
import com.dhcc.core.modules.system.cache.role.RoleCache;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.core.modules.system.service.IUserService;
import com.dhcc.core.modules.system.transfer.DeptRoleDto;

/**
 * 用户VO
 * 
 * @ClassName: UserVo
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:43:06
 */
public class UserVo {

    public UserVo() {
    }

    public UserVo(User user) {
        this.id = user.getId();
        this.loginName = user.getLoginName();
        this.userCode = user.getUserCode();
        this.name = user.getName();
        this.birthday = user.getBirthday();
        this.sex = user.getSex();
        this.email = user.getEmail();
        this.phone = user.getPhone();
        this.status = user.getStatus();
        this.avatar = user.getAvatar();
        IUserService userService = SpringContextHolder.getBean(IUserService.class);
        List<DeptRoleDto> drs = userService.getDeptRoles(user.getId());
        this.setDeptRoles(drs);
        if(CommonUtil.isNotEmpty(drs)){
            for (DeptRoleDto deptRoleDto : drs) {
                if(deptRoleDto.getIsPartTime()==0){
                    this.deptId = deptRoleDto.getDeptId();
                    this.roleIds = deptRoleDto.getRoleIds();
                    this.deptCode = DeptCache.me().getDeptCodeById(this.deptId);
                    this.roleCodes = RoleCache.me().getRoleCodesByIds(this.roleIds);
                    this.deptName = DeptCache.me().getDeptNameById(this.deptId);
                    this.roleNames = RoleCache.me().getRoleNamesByIds(this.roleIds);
                    break;
                }
            }
        }
    }

    public UserVo(User user,Long deptId) {
        this.id = user.getId();
        this.loginName = user.getLoginName();
        this.userCode = user.getUserCode();
        this.name = user.getName();
        this.birthday = user.getBirthday();
        this.sex = user.getSex();
        this.email = user.getEmail();
        this.phone = user.getPhone();
        this.status = user.getStatus();
        this.avatar = user.getAvatar();
        IUserService userService = SpringContextHolder.getBean(IUserService.class);
        List<DeptRoleDto> drs = userService.getDeptRoles(user.getId());
        this.setDeptRoles(drs);
        if(CommonUtil.isNotEmpty(drs)){
            for (DeptRoleDto deptRoleDto : drs) {
                if(CommonUtil.equals(deptRoleDto.getDeptId(),deptId)){
                    this.deptId = deptRoleDto.getDeptId();
                    this.roleIds = deptRoleDto.getRoleIds();
                    this.deptCode = DeptCache.me().getDeptCodeById(this.deptId);
                    this.roleCodes = RoleCache.me().getRoleCodesByIds(this.roleIds);
                    this.deptName = DeptCache.me().getDeptNameById(this.deptId);
                    this.roleNames = RoleCache.me().getRoleNamesByIds(this.roleIds);
                    break;
                }
            }
        }
    }

    private Long id;
    private String loginName;
    private String userCode;
    private String name;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    private Integer sex;
    private String email;
    private String phone;
    private Long deptId;//主部门
    private String roleIds;//主部门主角色
    private String deptCode;//主部门
    private String roleCodes;//主部门主角色
    private String deptName;//主部门
    private String roleNames;//主部门主角色
    private Integer status;
    private String avatar;
    List<DeptRoleDto> deptRoles;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public List<DeptRoleDto> getDeptRoles() {
        return deptRoles;
    }

    public void setDeptRoles(List<DeptRoleDto> deptRoles) {
        this.deptRoles = deptRoles;
    }

    public String getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(String roleIds) {
        this.roleIds = roleIds;
    }

    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public String getRoleCodes() {
        return roleCodes;
    }

    public void setRoleCodes(String roleCodes) {
        this.roleCodes = roleCodes;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getRoleNames() {
        return roleNames;
    }

    public void setRoleNames(String roleNames) {
        this.roleNames = roleNames;
    }


}
